import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * @author ShenTuZhiGang
 * @version 1.0.0
 * @email 1600337300@qq.com
 * @date 2021-08-21 15:29
 */
public class ExamNetEase2021082103 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        int min = Integer.MAX_VALUE;
        while (scanner.hasNext()){
            int x = scanner.nextInt();
            min = Math.min(min,x);
            list.add(x);
        }
        if (list.size()==0){
            System.out.println(0);
            return;
        }
        int x = 1;
        int ans =1;
        int d = list.indexOf(min);
        for (int i = 1; i <list.size() ; i++) {
            int j= (i+d)%list.size();
            if(list.get(j)>list.get((j-1 + list.size())%list.size())){
                x++;
            }else{
                x = 1;
            }
            ans+=x;
        }
        System.out.println(ans);
    }
}
